package com.tanhua.dubbo.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.dubbo.pojo.UserLike;
import com.tanhua.dubbo.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存喜欢的用户
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        long count = this.mongoTemplate.count(query, UserLike.class);
        if (count > 0) {
            return null;
        }

        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        userLike.setCreated(System.currentTimeMillis());

        this.mongoTemplate.save(userLike);
        return userLike.getId().toHexString();
    }

    /**
     * 判断双发是否互相喜欢，如果互相喜欢，则表中会有两条数据
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        // 双方互相喜欢
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        Query query = Query.query(criteria);

        long count = this.mongoTemplate.count(query, UserLike.class);
        return count == 2;
    }

    /**
     * 取消喜欢，不喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria
                .where("userId")
                .is(userId)
                .and("likeUserId").is(likeUserId));
        DeleteResult remove = this.mongoTemplate.remove(query, UserLike.class);
        return remove.getDeletedCount() == 1;
    }

    @Override
    public Long eachLikeCount(Long userId) {
        // 思路：先查找到自己喜欢的列表，再根据喜欢的用户id去查找喜欢我的人

        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> likeList = this.mongoTemplate.find(query, UserLike.class);

        if (CollectionUtils.isEmpty(likeList)) {
            return 0L;
        }

        // 封装我喜欢的用户id
        List<Long> likeUserIds = new ArrayList<>();
        for (UserLike userLike : likeList) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        // 查找喜欢我的人
        Query query2 = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));
        return this.mongoTemplate.count(query2, UserLike.class);
    }

    @Override
    public Long queryLikeCount(Long userId) {
        return this.mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        return this.mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class);
    }

    /**
     * 查找互相喜欢的列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {

        // 思路：先查找到自己喜欢的列表，再根据喜欢的用户id去查找喜欢我的人
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> likeList = this.mongoTemplate.find(query, UserLike.class);

        // 封装我喜欢的用户id
        List<Long> likeUserIds = new ArrayList<>();
        for (UserLike userLike : likeList) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        // 查找喜欢我的人
        PageRequest pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query2 = Query.query(Criteria.where("userId").in(likeUserIds)
                .and("likeUserId").is(userId)).with(pageable);
        return getUserLikePageInfo(page, pageSize, query2);
    }

    /**
     * 我喜欢的人
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return getUserLikePageInfo(page,pageSize,query);
    }

    /**
     * 喜欢我的人
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return getUserLikePageInfo(page,pageSize,query);
    }

    /**
     * 根据查询条件，获取封装userLike对象列表的分页对象
     *
     * @param page
     * @param pageSize
     * @param query
     * @return
     */
    private PageInfo<UserLike> getUserLikePageInfo(Integer page, Integer pageSize, Query query) {
        List<UserLike> userLikeList = this.mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(0);
        pageInfo.setRecords(userLikeList);
        return pageInfo;
    }
}
